package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.JsonResult;
import cn.wolfcode.luowowo.common.util.KeysUtil;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateService;
import cn.wolfcode.luowowo.search.vo.ESStatisVO;
import cn.wolfcode.luowowo.service.IStrateryStatisRedisService;
import cn.wolfcode.luowowo.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrateryStatisRedisService statisRedisService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyTemplateService strategyTemplateService;


    /**
     * 攻略首页
     *
     * @return
     */
    @RequestMapping("")
    public String guide(Model model) {
        //推荐排行
        model.addAttribute("commends",strategyCommendService.queryCommendTop5());
        model.addAttribute("abroadCds",statisRedisService.getCommendVO(KeysUtil.FOREIGN_STRATERY_STATIS_COMMEND_SORT.getName()));
        model.addAttribute("unabroadCds",statisRedisService.getCommendVO(KeysUtil.INLAND_STRATERY_STATIS_COMMEND_SORT.getName()));
        model.addAttribute("hotCds",statisRedisService.getCommendVO(KeysUtil.STRATERY_STATIS_HOT_COMMEND_SORT.getName()));
        model.addAttribute("themeCds",strategyTemplateService.getThemeCommend());

        //多条件查询
        //国内攻略   chinas    国外攻略   abroads   主题攻略   themes
        model.addAttribute("chinas",strategyTemplateService.getStatisVo("destId","destName",ESStatisVO.INLAND_STATIS_SEARCH));
        model.addAttribute("abroads",strategyTemplateService.getStatisVo("countryId","countryName",ESStatisVO.ABROAD_STATIS_SEARCH));
        model.addAttribute("themes",strategyTemplateService.getStatisVo("themeId","themeName",ESStatisVO.THEME_STATIS_SEARCH));

        return "strategy/index";
    }

    @RequestMapping("searchPage")
    public String guide(Model model, @ModelAttribute("qo")SearchQueryObject qo) {
        model.addAttribute("page", strategyTemplateService.queryBytype(qo));
        return "strategy/searchPageTpl";
    }

    /**
     *
     * 地区攻略列表
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String guide(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        List<Destination> list = destinationService.creatToasts(qo.getDestId());
        Destination dest = list.remove(list.size() - 1);
        model.addAttribute("toasts", list);
        model.addAttribute("dest", dest);
        model.addAttribute("tags", strategyTagService.list());
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategy/list";
    }

    /**
     * 具体攻略内容
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail")
    public String guide(Long id, Model model,@UserParam UserInfo userInfo) {
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        //操作阅读数和显示统计数
        statisRedisService.statisAndUpdateViewnum(id, 1,StatisVO.USER_FAVOR_STRATERY);
        StrategyContent content = strategyDetailService.getContent(id);
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail", strategyDetail);
        model.addAttribute("vo", statisRedisService.getStatisRedis(KeysUtil.STRATEGY_STATIS_VO.join(id.toString())));
        //判断是否收藏 页面回显
        if (userInfo != null){
            boolean isFavor = statisRedisService.checkFavor(id,
                    KeysUtil.USER_STRATERY_STATIS_FAVOR.join(strategyDetail.getId().toString()));
            model.addAttribute("isFavor", isFavor);
        }
        return "strategy/detail";
    }

    /**
     * 查询评论
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("comment")
    public String commentStrategy(@ModelAttribute("qo") StrategyCommentQuery qo, Model model) {
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @RequestMapping("commentAdd")
    @ResponseBody
    public JsonResult commentStrategy(StrategyComment comment, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new JsonResult(false, "请先登录");
        }
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());

        Long detailId = comment.getDetailId();
        StrategyDetail strategyDetail = strategyDetailService.get(detailId);
        comment.setDetailTitle(strategyDetail.getTitle());
        strategyCommentService.saveOrUpdate(comment);

        //攻略评论数加1
        statisRedisService.setReplynumStatisRedis(detailId, 1,KeysUtil.STRATEGY_STATIS_VO.join(detailId.toString()));
        StatisVO vo = statisRedisService.getStatisRedis(KeysUtil.STRATEGY_STATIS_VO.join(detailId.toString()));
        return JsonResult.SUCCESS.addData(vo.getReplynum());
    }

    /**
     * 点赞攻略评论
     *
     * @param toid   评论id
     * @param fromid 点赞用户的id
     * @return
     */
    @RequestMapping("commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        //先查评论id中的点赞集合
        StrategyComment c = strategyCommentService.commentThumbUp(toid, fromid);
        strategyCommentService.saveOrUpdate(c);
        return JsonResult.SUCCESS;
    }


    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        return userInfo == null ? JsonResult.UNLOGIN : statisRedisService.favor(sid, userInfo.getId(),StatisVO.USER_FAVOR_STRATERY);
    }

    @RequestMapping("strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        return userInfo == null ? JsonResult.UNLOGIN : statisRedisService.strategyThumbup(sid, userInfo.getId(),StatisVO.USER_FAVOR_STRATERY);
    }

}
